package com.mxpio.erp.plan.mrp.policy.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.mxpio.erp.common.quality.entity.InQualityOrder;
import com.mxpio.erp.common.quality.entity.InQualityOrderLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mxpio.erp.common.enums.OrderEnums.AccessType;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.purchase.entity.BuyRequestOrderLine;
import com.mxpio.erp.common.purchase.entity.PurchaseOrder;
import com.mxpio.erp.common.purchase.entity.PurchaseOrderLine;
import com.mxpio.erp.common.purchase.entity.ReceivingNotice;
import com.mxpio.erp.common.purchase.entity.ReceivingNoticeLine;
import com.mxpio.erp.common.purchase.entity.RejectNotice;
import com.mxpio.erp.common.purchase.entity.RejectNoticeLine;
import com.mxpio.erp.common.vo.NoticeClassVo;
import com.mxpio.erp.plan.mrp.Context;
import com.mxpio.erp.plan.mrp.CountModelMap;
import com.mxpio.erp.plan.mrp.bo.CountModel;
import com.mxpio.erp.plan.mrp.policy.LrpPurchaseOrderPolicy;
import com.mxpioframework.jpa.JpaUtil;

/**
 * 计算采购订单未完成数量 
 * @author maxp
 *
 */
@Component
public class PurchaseOrderPolicy extends LrpPurchaseOrderPolicy {
	
	@Autowired
    private OrderLineService bizOrderLineService;

	@Override
	public void apply(Context context) {
		CountModelMap<List<CountModel>> result = new CountModelMap<List<CountModel>>();
		
		List<PurchaseOrderLine> poLines = JpaUtil.linq(PurchaseOrderLine.class)
				.exists(PurchaseOrder.class)
				.equalProperty("bizNo", "bizNo")
					.equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())
					.equal("closeStatus", OrderStatusEnums.CloseStatus.OPEN.getStatusCode())
					.notEqual("orderStatus", OrderStatusEnums.OrderStatus.FINISHED.getStatusCode())
					.notEqual("orderStatus", OrderStatusEnums.OrderStatus.CLEARED.getStatusCode())
				.end()
				.list();
		List<NoticeClassVo> noticeClassVos = new ArrayList<>();
		noticeClassVos.add(new NoticeClassVo(ReceivingNotice.class, ReceivingNoticeLine.class));
		noticeClassVos.add(new NoticeClassVo(RejectNotice.class, RejectNoticeLine.class));
		noticeClassVos.add(new NoticeClassVo(InQualityOrder.class, InQualityOrderLine.class));//增加入库验收单
		bizOrderLineService.handleBizOrderLineQuantity(poLines, noticeClassVos, AccessType.IN.getCode());
		for(PurchaseOrderLine poLine : poLines){
			try{
				
				BigDecimal lastQuantity = poLine.getQuantity().subtract(poLine.getActualQuantity());//取数由计划数量改为订单数量
				if(lastQuantity.compareTo(BigDecimal.ZERO)>0){
					CountModel countMode = new CountModel();
					countMode.setCount(lastQuantity);
					countMode.setOrderDate(poLine.getSuggestArriveDate());
					countMode.setItemCode(poLine.getItemCode());
					countMode.setType(3);
					
					indexProcessAdd(context, poLine, result, countMode);
				}
			}catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
		
		// 申购单统计
		List<BuyRequestOrderLine> brs = JpaUtil.linq(BuyRequestOrderLine.class).list();
		for(BuyRequestOrderLine br : brs){
			try {
				CountModel countMode = new CountModel();
				countMode.setCount(br.getQuantity());
				countMode.setOrderDate(br.getArriveDate());
				countMode.setItemCode(br.getItemCode());
				countMode.setType(3);
				
				indexProcessAdd(context, br, result, countMode);
			}catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
			/*
			 * List<CountModel> countModelList = purchaseOrderCount.get(br.getItemCode());
			 * if(countModelList == null){ countModelList = new ArrayList<>(); }
			 * 
			 * countModelList.add(countMode); purchaseOrderCount.put(br.getItemCode(),
			 * countModelList);
			 */
		}
		
		context.setPurchaseOrderCount(result);
	}

}
